//
//  MetaPublicController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "MetaPublicController.h"
#import "FormattingUtilities.h"
#import "GSSCommunicator.h"
#import "GSSObject.h"

@implementation MetaPublicController

- (void)dealloc {
    [publicSwitch release];
    [super dealloc];
}

- (NSInteger)numberOfRows {
    return 1;
}

- (UITableViewCell *)cellForRow:(NSInteger)row {
    static NSString *CellIdentifier = @"MetaPublicCell";
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (!publicSwitch) {
        publicSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [publicSwitch addTarget:self
                         action:@selector(publicChanged)
               forControlEvents:UIControlEventValueChanged];                
    }
    
    publicSwitch.on = object.readForAll;
    publicSwitch.enabled = self.isEditing;
    
    cell.textLabel.text = LocalizedString(@"Public");
    cell.accessoryView = publicSwitch;
    cell.editingAccessoryView = publicSwitch;
    
    return cell;
}

//MARK: -

- (void)publicChanged {
    [[GSSCommunicator defaultCommunicator] setReadForAll:publicSwitch.on ofObject:object];
    object.readForAll = publicSwitch.on;
    [object save];
    [self.tableView reloadData];
}

@end
